<?php

namespace app\controller;

use app\BaseController;
use app\service\PartnerRoleService;
use app\service\PartnerUserService;
use think\exception\ValidateException;
use think\facade\Request;

/**
 * 合作商角色
 */
class Partnerrole extends BaseController
{
    //合作商角色列表
    public function role_lists()
    {
        $uid = Request::param('uid/d', 0);
        $param['partner_id'] = get_partner_id($uid);
        $data = PartnerUserService::getAllPartnerUser($param);
        returnSuccess(200, '登录成功', $data);
    }

    //合作商角色列表
    public function lists()
    {
        $uid = Request::param('uid/d', 0);
        $param['partner_id'] = get_partner_id($uid);
        $data = PartnerRoleService::getAllRole($param);
        returnSuccess(200, '登录成功', $data);
    }

    //添加合作商角色
    public function add_role()
    {
        $uid = Request::param('uid/d', 0);
        $param['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['name'] = Request::param('name/s', '');
        $param['pid'] = Request::param('pid/d', 0);
        $param['permission_ids'] = Request::param('permission_ids/s', '');
        $param['status'] = Request::param('status/d', 1);
        $param['ip'] = Request::ip();
        try {
            validate(\app\validate\PartnerRole::class)->check([
                'permission_ids' => $param['permission_ids'],
                'pid' => $param['pid'],
                'name' => $param['name'],
            ]);
        } catch (ValidateException $e) {
            returnFail(21009, $e->getError());
        }
        $isMatched = preg_match('/^\d+(,\d+)*$/', $param['permission_ids'], $matches);
        if (!$isMatched) {
            returnFail(206, '权限内容格式不正确');
        }
        $resCode = PartnerRoleService::add_role($param);
        if ($resCode == 10000) {
            returnSuccess(200, '添加成功');
        } elseif ($resCode == 10001) {
            returnFail(204, '此角色名称已存在');
        } else {
            returnFail(205, '添加失败');
        }
    }

    //修改合作商角色
    public function modify_role()
    {
        $uid = Request::param('uid/d', 0);
        $param['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['name'] = Request::param('name/s', '');
        $param['pid'] = Request::param('pid/d', 0);
        $param['permission_ids'] = Request::param('permission_ids/s', '');
        $param['status'] = Request::param('status/d', 1);
        $role_id = Request::param('role_id/d', 0);
        $param['ip'] = Request::ip();
        try {
            validate(\app\validate\PartnerRole::class)->check([
                'permission_ids' => $param['permission_ids'],
                'pid' => $param['pid'],
                'name' => $param['name'],
            ]);
        } catch (ValidateException $e) {
            returnFail(21009, $e->getError());
        }
        if ($role_id == 0) {
            returnFail(206, '请求参数不正确');
        }
        $isMatched = preg_match('/^\d+(,\d+)*$/', $param['permission_ids'], $matches);
        if (!$isMatched) {
            returnFail(206, '权限内容格式不正确');
        }
        $resCode = PartnerRoleService::modify_role($param, $role_id);
        if ($resCode == 10000) {
            returnSuccess(200, '修改成功');
        } elseif ($resCode == 10001) {
            returnFail(204, '此角色名称不存在');
        } else {
            returnFail(205, '修改失败');
        }
    }

    //删除角色
    public function del_role()
    {
        $uid = Request::param('uid/d', 0);
        $param['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $role_id = Request::param('role_id/d', 0);
        $param['ip'] = Request::ip();
        if ($role_id == 0) {
            returnFail(206, '请求参数不正确');
        }
        $resCode = PartnerRoleService::del_role($param, $role_id);
        if ($resCode == 10000) {
            returnSuccess(200, '删除成功');
        } elseif ($resCode == 10001) {
            returnFail(204, '此角色名称不存在');
        } else {
            returnFail(205, '删除失败');
        }
    }
}
